/*
 * @Description:
 * @Author: Zzt
 * @Date: 2022-12-28 10:02:12
 * @LastEditTime: 2023-01-05 18:51:28
 */
import {
  SELECT_CHANGE,
  M_SELECT_KEY,
  SET_SELECT_FOND,
  SET_SELECT_AT,
  SET_FIRST_AT,
  SET_TOP_LIST,
  SET_DOWN_LIST,
  DELETE_ELIST_ITEM,
  DELETE_LIST_ITEM,
  ADD_ELIST_ITEM
} from '../constants';
import { ADD_LIST_ITEM, EDIT_LIST_ITEM } from '@/components/LevelTable/constants';
import { editItemById, insertItem, delItemByIdArray } from '@/utils/list';

export const ArchiveCollectionReducer = (state, action) => {
  switch (action.type) {
    /**
     * @description: 设置选中全宗
     */
    case SET_SELECT_FOND:
      return {
        ...state,
        selectFond: action.data
      };

    /**
     * @description: 设置选中的档案类型
     */
    case SET_SELECT_AT:
      return {
        ...state,
        selectAT: action.data
      };
    /* 获取文件级列表 */
    case SET_TOP_LIST: {
      return {
        ...state,
        recordsData: action.data
      };
    }
    case SET_DOWN_LIST: {
      return { ...state, eRecordsData: action.data };
    }
    /**
     * @description: 增加文件级列表条目
     */
    case ADD_LIST_ITEM: {
      const { data } = action;
      console.log('state.recordsData.data', data, state.recordsData);
      const list = insertItem(data, state.recordsData.data);

      return {
        ...state,
        recordsData: {
          ...state.recordsData,
          data: list
        }
      };
    }

    /**
     * @description: 编辑文件级列表条目
     */
    case EDIT_LIST_ITEM: {
      const { id, data } = action;
      const list = editItemById(id, data, state.recordsData.data);
      return {
        ...state,
        recordsData: {
          ...state.recordsData,
          data: list
        }
      };
    }
    case DELETE_LIST_ITEM: {
      const list = delItemByIdArray(action.ids, state.recordsData.data);
      return {
        ...state,
        recordsData: {
          ...state.recordsData,
          data: list
        }
      };
    }
    case DELETE_ELIST_ITEM: {
      const list = delItemByIdArray(action.ids, state.eRecordsData.data);
      return {
        ...state,
        eRecordsData: {
          ...state.eRecordsData,
          data: list
        }
      };
    }
    case ADD_ELIST_ITEM: {
      const { data } = action;
      const list = insertItem(data, state.eRecordsData.data);
      return {
        ...state,
        eRecordsData: {
          ...state.eRecordsData,
          data: list
        }
      };
    }
    /**
     * @description: 设置选中的档案类型
     */
    case SET_FIRST_AT:
      return {
        ...state,
        selectAT: action.data?.length > 0 ? action.data[0] : {}
      };

    case SELECT_CHANGE:
      return { ...state, selectChange: action.data };

    case M_SELECT_KEY:
      return { ...state, mSelectKey: action.data };
    // case DELETE_ELIST_ITEM:
    //   return {...state,};
    default:
      console.log('untouched', action);
      return state;
  }
};
